package com.siriframe.user.service.impl;

import com.siriframe.entity.PageResponse;
import com.siriframe.user.convert.UserConverter;
import com.siriframe.user.dao.UserDao;
import com.siriframe.user.entity.dto.UserDto;
import com.siriframe.user.entity.po.User;
import com.siriframe.user.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Long id) {
        return this.userDao.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(User user) {
        return this.userDao.insert(user);
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public int update(User user) {
        return this.userDao.update(user);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.userDao.deleteById(id) > 0;
    }

    @Override
    public Long queryCount(Map<String, Object> conditions) {
        User user = new User();
        return userDao.count(user);
    }

    @Override
    public PageResponse<User> queryByPage(UserDto userDto) {
        User user = UserConverter.INSTANCE.convertUserDtoToUserPo(userDto);
        PageResponse<User> pageResponse = new PageResponse<>();
        pageResponse.setCurrent(userDto.getPageNo());
        pageResponse.setPageSize(userDto.getPageSize());
        Long pageStart = (userDto.getPageNo() - 1) * userDto.getPageSize();
        long total = this.userDao.count(user);
        List<User> userList = this.userDao.queryAllByLimit(user, pageStart, userDto.getPageSize());
        pageResponse.setTotal(total);
        pageResponse.setRecords(userList);
        return pageResponse;
    }
}
